package com.hualan.sfzy.controller.course;

import com.hualan.sfzy.entity.Course;
import com.hualan.sfzy.entity.SkilledFiled;
import com.hualan.sfzy.service.course.CourseService;
import com.hualan.sfzy.utils.Result;
import com.hualan.sfzy.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @PostMapping
    public Result add(@RequestBody CourseVo entity){
        int result = courseService.add(entity);
        if (result>0)
            return Result.success(result);
        return Result.error("添加失败");
    }
    @GetMapping
    public Result find(){
        List<Course> list = courseService.find();
        if (list!=null)
            return Result.success(list);
        return Result.error("查询失败");
    }

    @GetMapping("/skilledFiled")
    public Result findSkilledFiled(){
        List<SkilledFiled> list = courseService.findSkilledFiled();
        if (list!=null)
            return Result.success(list);
        return Result.error("查询失败");
    }
    @GetMapping("/counselor")
    public Result findCounselorId(@RequestParam(required = true) Integer skillId){
        Integer[] ids = courseService.findCounselorId(skillId);
        if (ids!=null)
            return Result.success(ids);
        return Result.error("查询失败");
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Course course = courseService.findById(id);
        if (course!=null)
            return Result.success(course);
        return Result.error("查询失败");
    }

    @PostMapping("/page")
    public Result findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody(required = false) CourseVo entity){
        System.out.println(entity);
        return Result.success(courseService.findPage(pageNum, pageSize, entity));
    }
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Integer id){
        int result = courseService.remove(id);
        if (result>0)
            return Result.success(result);
        return Result.error("删除失败");
    }
    @DeleteMapping("/batch")
    public Result removeBatch(Integer[] ids){
        int result = courseService.removeBatch(ids);
        if (result>0)
            return Result.success(result);
        return Result.error("删除失败");
    }

    @PutMapping("/update")
    public Result editStudyNum(Integer courseId,Integer num){
        int result = courseService.editStudyNum(courseId, num);
        if (result>0)
            return Result.success(result);
        return Result.error("修改失败");
    }



    @PutMapping
    public Result edit(@RequestBody CourseVo entity){
        int result = courseService.edit(entity);
        if (result>0)
            return Result.success(result);
        return Result.error("修改失败");
    }
}
